<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>原料仓库管理系统</title>
</head>
<link rel="stylesheet" type="text/css" href="css/zdy_ok.css" />
<body>
<a href="CKGL_zdy.php">返回上一页</a>
<div class="box">

<table align="center" width="95%" border="1" cellpadding="1" cellspacing="1" bordercolor="#000000" bgcolor="#c3c3c3">

<?php
require('lianjie.php');
$sum=0;
$dt=array();
$sum_t=0;
if(isset($_POST['time_qian'])){
	$time_qian=$_POST['time_qian'];
}
if(isset($_POST['time_hou'])){
	$time_hou=$_POST['time_hou'];
	array_push($dt,'时间');
	$sum_t=$sum_t+1;
}
if(isset($_POST['rck_xz'])){
	$rck_xz=$_POST['rck_xz'];
}

if(isset($_POST['a'])){
	$a=$_POST['a'];
	for($i=0;$i<count($a);$i++){
		if($a[$i]=='PH'){
			array_push($dt,'批号');
			$sum_t=$sum_t+1;
		}
		if($a[$i]=='name'){
			array_push($dt,'名称');
			$sum_t=$sum_t+1;
		}
		if($a[$i]=='id'){
			array_push($dt,'编号');
			$sum_t=$sum_t+1;
		}
		if($a[$i]=='car_id'){
			array_push($dt,'合同号/车牌号');
			$sum_t=$sum_t+1;
		}
		if($a[$i]=='DJ_id'){
			array_push($dt,'单据号');
			$sum_t=$sum_t+1;
		}
	}
}
if(isset($_POST['s'])){
	$s=$_POST['s'];
	for($i=0;$i<count($s);$i++){
		if($s[$i]=='SW'){
			array_push($dt,'实物量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='GU'){
			array_push($dt,'钴量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='NIE'){
			array_push($dt,'镍量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='MENG'){
			array_push($dt,'锰量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='TONG'){
			array_push($dt,'铜量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='LI'){
			array_push($dt,'锂量');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='BS'){
			array_push($dt,'包数');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='LB1'){
			array_push($dt,'类别1');
			$sum_t=$sum_t+1;
		}
		if($s[$i]=='LB2'){
			array_push($dt,'类别2');
			$sum_t=$sum_t+1;
		}
	}
}
if(isset($_POST['ku'])){
	array_push($dt,'库区');
	array_push($dt,'储位');
	$sum_t=$sum_t+2;
}
if(isset($_POST['YLLB'])){
	array_push($dt,'原料类别');
	$sum_t=$sum_t+1;
}
if(isset($_POST['r'])){
	array_push($dt,'表类别');
	$sum_t=$sum_t+1;
}
if(isset($_POST['z'])){
	array_push($dt,'两大类别');
	$sum_t=$sum_t+1;
}
//echo $sum_t;
?>
<thead>
<?php
for($i=0;$i<$sum_t;$i++){
	?>
    <th><?php echo $dt[$i]; ?></th>
    <?php
}
?>
</thead>
<?php
if($rck_xz=='xclb'){
	$a=$_POST['a'];
	$sqla='';
	for($i=0;$i<count($a);$i++){
		if($i==count($a)-1){
			$sqla=$sqla."r.".$a[$i];}
		else{$sqla=$sqla."r.".$a[$i].",";}
		$sum++;
	}
	$suma=$sum;
	//echo $sqla.',';
	$s=$_POST['s'];
	$sqls='';
	for($i=0;$i<count($s);$i++){
		if($i==count($s)-1){
			$sqls=$sqls."(r.".$s[$i]."-ifnull(c.".$s[$i].",0))";}
		else{$sqls=$sqls."(r.".$s[$i]."-ifnull(c.".$s[$i].",0)),";}
		$sum=$sum+1;
	}
	$sums=$sum;
	//echo $sqls;
	if(isset($_POST['ku'])){
		$sqlku=',r.KQ,r.CW';
		$ku=$_POST['ku'];
		$whereku='(';
		$sum=$sum+2;
		for($i=0;$i<count($ku);$i++){
		if($i==count($ku)-1){
			$whereku=$whereku."r.KQ='".$ku[$i]."')";}
		else{$whereku=$whereku."r.KQ='".$ku[$i]."' or ";}
		}
	}
	else{$whereku='r.KQ=r.KQ';$sqlku='';}
	//echo $sqlku.$whereku;
	if(isset($_POST['YLLB'])){
		$sqlyllb=',r.YLLB';
		$yllb=$_POST['YLLB'];
		$whereyllb='(';
		$sum=$sum+1;
		for($i=0;$i<count($yllb);$i++){
		if($i==count($yllb)-1){
			$whereyllb=$whereyllb."r.YLLB='".$yllb[$i]."')";}
		else{$whereyllb=$whereyllb."r.YLLB='".$yllb[$i]."' or ";}
		}
	}
	else{$whereyllb='r.YLLB=r.YLLB';$sqlyllb='';}
	//echo $sqlyllb.$whereyllb.$sum;
	if(isset($_POST['r'])){
		$sqlblb=',r.BLB';
		$blb=$_POST['r'];
		$whereblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($blb);$i++){
		if($i==count($blb)-1){
			$whereblb=$whereblb."r.BLB='".$blb[$i]."')";}
		else{$whereblb=$whereblb."r.BLB='".$blb[$i]."' or ";}
		}
	}
	else{$whereblb='r.BLB=r.BLB';$sqlblb='';}
	
	if(isset($_POST['z'])){
		$sqllblb=',r.LBLB';
		$lblb=$_POST['z'];
		$wherelblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($lblb);$i++){
		if($i==count($lblb)-1){
			$wherelblb=$wherelblb."r.LBLB='".$lblb[$i]."')";}
		else{$wherelblb=$wherelblb."r.LBLB='".$lblb[$i]."' or ";}
		}
	}
	else{$wherelblb='r.LBLB=r.LBLB';$sqllblb='';}
	//echo $sqllblb.$wherelblb.$sum;
	$sql="select r.dh_date,".$sqla.",".$sqls.$sqlku.$sqlyllb.$sqlblb.$sqllblb." from (select dh_date,name,PH,id,KQ,CW,car_id,DJ_id,sum(BS) as BS,sum(SW) as SW,sum(GU) as GU,sum(NIE) as NIE,sum(MENG) as MENG,sum(TONG) as TONG,sum(LI) as LI,BLB,YLLB,LY,LBLB,LB1,LB2 from rkb group by name,PH,id) as r left join (select name,PH,id,car_id,DJ_id,sum(SW) as SW,sum(GU) as GU,sum(NIE) as NIE,sum(MENG) as MENG,sum(TONG) as TONG,sum(LI) as LI,sum(BS) as BS,QX,LB1,LB2 from ckb where dh_date>='".$time_qian."' and dh_date<='".$time_hou."' group by name,PH,id) as c on r.name=c.name and r.PH=c.PH and r.id=c.id where (".$whereku." and ".$whereyllb." and ".$whereblb." and ".$wherelblb.") and r.dh_date>='".$time_qian."' and r.dh_date<='".$time_hou."';";
	//echo $sql;
	if(mysqli_select_db($connID,$dbName)){
		$result=mysqli_query($connID,$sql);
		while($myrow=mysqli_fetch_array($result)){
			?>
			<tr>
            <?php 
			for($i=0;$i<=$sum;$i++){
				?>
                <td><?php echo $myrow[$i];?></td>
                <?php
			}
			?>
            </tr>
			<?php
		}
	}
}
else if($rck_xz=='rkb'){
	$a=$_POST['a'];
	$sqla='';
	$suma=1;
	$sums=1;
	for($i=0;$i<count($a);$i++){
		if($i==count($a)-1){
			$sqla=$sqla.$a[$i];}
		else{$sqla=$sqla.$a[$i].",";}
		$sum++;
	}
	//echo $sqla.',';
	$s=$_POST['s'];
	$sqls='';
	for($i=0;$i<count($s);$i++){
		if($i==count($s)-1){
			$sqls=$sqls.$s[$i];}
		else{$sqls=$sqls.$s[$i].",";}
		$sum++;
	}
	//echo $sqls;
	if(isset($_POST['ku'])){
		$sqlku=',KQ,CW';
		$ku=$_POST['ku'];
		$whereku='(';
		$sum=$sum+2;
		for($i=0;$i<count($ku);$i++){
		if($i==count($ku)-1){
			$whereku=$whereku."KQ='".$ku[$i]."')";}
		else{$whereku=$whereku."KQ='".$ku[$i]."' or ";}
		}
	}
	else{$whereku='KQ=KQ';$sqlku='';}
	//echo $sqlku.$whereku;
	if(isset($_POST['YLLB'])){
		$sqlyllb=',YLLB';
		$yllb=$_POST['YLLB'];
		$whereyllb='(';
		$sum=$sum+1;
		for($i=0;$i<count($yllb);$i++){
		if($i==count($yllb)-1){
			$whereyllb=$whereyllb."YLLB='".$yllb[$i]."')";}
		else{$whereyllb=$whereyllb."YLLB='".$yllb[$i]."' or ";}
		}
	}
	else{$whereyllb='YLLB=YLLB';$sqlyllb='';}
	//echo $sqlyllb.$whereyllb.$sum;
	if(isset($_POST['r'])){
		$sqlblb=',BLB';
		$blb=$_POST['r'];
		$whereblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($blb);$i++){
		if($i==count($blb)-1){
			$whereblb=$whereblb."BLB='".$blb[$i]."')";}
		else{$whereblb=$whereblb."BLB='".$blb[$i]."' or ";}
		}
	}
	else{$whereblb='BLB=BLB';$sqlblb='';}
	
	if(isset($_POST['z'])){
		$sqllblb=',LBLB';
		$lblb=$_POST['z'];
		$wherelblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($lblb);$i++){
		if($i==count($lblb)-1){
			$wherelblb=$wherelblb."LBLB='".$lblb[$i]."')";}
		else{$wherelblb=$wherelblb."LBLB='".$lblb[$i]."' or ";}
		}
	}
	else{$wherelblb='LBLB=LBLB';$sqllblb='';}
	//echo $sqllblb.$wherelblb.$sum;
	$sql="select dh_date,".$sqla.",".$sqls.$sqlku.$sqlyllb.$sqlblb.$sqllblb." from rkb where (".$whereku." and ".$whereyllb." and ".$whereblb." and ".$wherelblb.") and dh_date>='".$time_qian."' and dh_date<='".$time_hou."';";
	//echo $sql;
	if(mysqli_select_db($connID,$dbName)){
		$result=mysqli_query($connID,$sql);
		while($myrow=mysqli_fetch_array($result)){
			?>
			<tr>
            <?php 
			for($i=0;$i<=$sum;$i++){
				?>
                <td><?php echo $myrow[$i];?></td>
                <?php
			}
			?>
            </tr>
			<?php
		}
	}
}
else if($rck_xz=='ckb'){
	$a=$_POST['a'];
	$sqla='';
	$suma=1;
	$sums=1;
	for($i=0;$i<count($a);$i++){
		if($i==count($a)-1){
			$sqla=$sqla.$a[$i];}
		else{$sqla=$sqla.$a[$i].",";}
		$sum++;
	}
	//echo $sqla.',';
	$s=$_POST['s'];
	$sqls='';
	for($i=0;$i<count($s);$i++){
		if($i==count($s)-1){
			$sqls=$sqls.$s[$i];}
		else{$sqls=$sqls.$s[$i].",";}
		$sum++;
	}
	//echo $sqls;
	if(isset($_POST['ku'])){
		$sqlku=',KQ,CW';
		$ku=$_POST['ku'];
		$whereku='(';
		$sum=$sum+2;
		for($i=0;$i<count($ku);$i++){
		if($i==count($ku)-1){
			$whereku=$whereku."KQ='".$ku[$i]."')";}
		else{$whereku=$whereku."KQ='".$ku[$i]."' or ";}
		}
	}
	else{$whereku='KQ=KQ';$sqlku='';}
	//echo $sqlku.$whereku;
	if(isset($_POST['YLLB'])){
		$sqlyllb=',YLLB';
		$yllb=$_POST['YLLB'];
		$whereyllb='(';
		$sum=$sum+1;
		for($i=0;$i<count($yllb);$i++){
		if($i==count($yllb)-1){
			$whereyllb=$whereyllb."YLLB='".$yllb[$i]."')";}
		else{$whereyllb=$whereyllb."YLLB='".$yllb[$i]."' or ";}
		}
	}
	else{$whereyllb='YLLB=YLLB';$sqlyllb='';}
	//echo $sqlyllb.$whereyllb.$sum;
	if(isset($_POST['r'])){
		$sqlblb=',BLB';
		$blb=$_POST['r'];
		$whereblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($blb);$i++){
		if($i==count($blb)-1){
			$whereblb=$whereblb."BLB='".$blb[$i]."')";}
		else{$whereblb=$whereblb."BLB='".$blb[$i]."' or ";}
		}
	}
	else{$whereblb='BLB=BLB';$sqlblb='';}
	
	if(isset($_POST['z'])){
		$sqllblb=',LBLB';
		$lblb=$_POST['z'];
		$wherelblb='(';
		$sum=$sum+1;
		for($i=0;$i<count($lblb);$i++){
		if($i==count($lblb)-1){
			$wherelblb=$wherelblb."LBLB='".$lblb[$i]."')";}
		else{$wherelblb=$wherelblb."LBLB='".$lblb[$i]."' or ";}
		}
	}
	else{$wherelblb='LBLB=LBLB';$sqllblb='';}
	//echo $sqllblb.$wherelblb.$sum;
	$sql="select dh_date,".$sqla.",".$sqls.$sqlku.$sqlyllb.$sqlblb.$sqllblb." from ckb where (".$whereku." and ".$whereyllb." and ".$whereblb." and ".$wherelblb.") and dh_date>='".$time_qian."' and dh_date<='".$time_hou."';";
	//echo $sql;
	if(mysqli_select_db($connID,$dbName)){
		$result=mysqli_query($connID,$sql);
		while($myrow=mysqli_fetch_array($result)){
			?>
			<tr>
            <?php 
			for($i=0;$i<=$sum;$i++){
				?>
                <td><?php echo $myrow[$i];?></td>
                <?php
			}
			?>
            </tr>
			<?php
		}
	}
}
$arr=array(0=>$sum,1=>"".$sql."",2=>$dt);
$url1='Excel_zdy_dy.php?'.http_build_query($arr);
?>
</table>
</div>
<a href="<?php echo $url1;?>"><div class="tz">导出</div></a>

</body>
</html>